#ifndef __STEPMOTOR_H
#define __STEPMOTOR_H

#include "stm32f10x_conf.h"

#define T1_FREQ           1000000 // 频率ft值

/*电机单圈参数*/
#define STEP_ANGLE				1.8									//步进电机的步距角 单位：度
#define FSPR              (360.0/1.8)         //步进电机的一圈所需脉冲数

#define MICRO_STEP        1          				//细分器细分数 
#define SPR               (FSPR*MICRO_STEP)   //细分后一圈所需脉冲数

/*数学常数,用于简化计算*/
#define ALPHA             ((float)(2*3.14159/SPR))       // α= 2*pi/spr
#define A_T               ((float)(ALPHA*T1_FREQ))
#define T1_FREQ_FIX       ((float)(T1_FREQ*0.69)) // 0.69为误差修正值(计算过程，文档中有写)

typedef enum{
    STOP=0,
    ACCEL=1,
    RUN=2,
    DECEL=3
}StepMotorState;

typedef struct{
    long step;
    long accel_count;//加速计数
    long accel_lim;//加速度曲线与减速度曲线的交点
    long max_s_lim;//达到设定速度时的步数
    long decel_val;//实际减速的步数
    long decel_start;//开始减速时的步数
    long step_delay;
    long min_delay;
    StepMotorState state;
    uint8_t step_flag;
    uint8_t dir;
    uint8_t en;
    uint8_t num;
}StepMotor_Struct;



extern StepMotor_Struct StepMotor_1;

void StepMotor_Init(void);
void StepMotor_Enable(StepMotor_Struct* user_struct);
void StepMotor_Disable(StepMotor_Struct* user_struct);
void StepMotor_Forward(StepMotor_Struct* user_struct);
void StepMotor_Backward(StepMotor_Struct* user_struct);
void StepMotor_Start(StepMotor_Struct* user_struct);
void StepMotor_Stop(StepMotor_Struct* user_struct);
void StepMotor_RunStep(StepMotor_Struct* user_struct,long step);
void StepMotor_MoveStep(StepMotor_Struct* user_struct,int32_t step,float accel,float decel,uint32_t speed);

#endif // !__STEPMOTOR_H

